<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>天空盒子</title>
    <script src="http://www.yanhuangxueyuan.com/versions/threejsR92/build/three.min.js"></script>
    <script src="http://www.yanhuangxueyuan.com/versions/threejsR92/examples/js/controls/OrbitControls.js"></script>
    <link rel="stylesheet" href="common.css">
</head>

<body>

</body>
<script>
    // 创建场景
    let scene = new THREE.Scene();
    var plane = createPlaneGeometryBasicMaterial(); //创建草地
    scene.add(plane);

    // 创建天空盒子
    var cubeTexture = new THREE.CubeTextureLoader()
        .setPath('image/sky/')
        .load(
            ['Sunny2_left.jpg', 'Sunny2_right.jpg', 'Sunny2_up.jpg', 'Sunny2_down.jpg', 'Sunny2_front.jpg', 'Sunny2_back.jpg', ]
        );
    scene.background = cubeTexture;

    // 光源
    let AmbientLight = new THREE.AmbientLight(0xffffff);
    scene.add(AmbientLight);

    // 相机
    var width = window.innerWidth; //窗口宽度
    var height = window.innerHeight; //窗口高度
    camera = new this.THREE.PerspectiveCamera(45, width / height, 1, 1000000)
    camera.position.set(0, 150, 1000) // 设置相机位置
    camera.lookAt(new this.THREE.Vector3(0, 0, 0)); // 设置相机方向(指向的场景对象)
    var axisHelper = new THREE.AxisHelper(600); // 辅助坐标系
    // scene.add(axisHelper);



    // 创建地面
    function createPlaneGeometryBasicMaterial() {
        var textureLoader = new THREE.TextureLoader();
        var cubeMaterial = new THREE.MeshStandardMaterial({
            map: textureLoader.load("image/floor.jpg"),
        });
        var planeGeometry = new THREE.PlaneGeometry(2000, 1000);
        var plane = new THREE.Mesh(planeGeometry, cubeMaterial);
        plane.rotation.x = -Math.PI / 2
        plane.position.y = 0;
        return plane;
    }


    // 渲染
    var renderer = new THREE.WebGLRenderer();
    document.body.appendChild(renderer.domElement); //body元素中插入canvas对象
    renderer.setSize(width, height); //设置渲染区域尺寸
    renderer.setClearColor(0xb9d3ff, 1) // 设置背景颜色
    var controls = new THREE.OrbitControls(camera, document.body) // 创建控件对象
    controls.addEventListener('change', render)

    function render() {
        requestAnimationFrame(render);
        renderer.render(scene, camera);
    }
    render()

    // 自适应渲染
    window.onresize = function() {
        renderer.setSize(window.innerWidth, window.innerHeight);
        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();
    };
</script>

</html>